package cn.edu.gzist.chapter09.job;

import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;

@PersistJobDataAfterExecution // 有状态的JobDetail
@DisallowConcurrentExecution // 防止同一个任务示例被并发执行
public class MyTaskJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //输出任务名字
        JobDetail jobDetail = jobExecutionContext.getJobDetail();

        System.out.println("任务名称："+jobDetail.getKey().getName());
        System.out.println("任务组："+jobDetail.getKey().getGroup());
        System.out.println("任务类："+jobDetail.getKey().getClass().getName());
        System.out.println("任务本次时间："+jobExecutionContext.getFireTime());
        System.out.println("任务下次时间："+jobExecutionContext.getNextFireTime());

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 记录运行的次数
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Integer id = (Integer) jobDataMap.get("count");
        System.out.println("运行的次数： "+id);
        jobDataMap.put("count", id+1);
    }
}
